#include "mainwindow.h"
#include "./ui_mainwindow.h"

#include <QThread>
#include <QDir>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setWindowTitle("地图综合应用");
    //setFixedSize(1200, 675);

    ui->lineEditLongitude->setText("116.404");
    ui->lineEditLatitude->setText("39.915");
    ui->lineEditCurAddress->setText("天安门广场");

    //load map
    LoadMap("map.html");

    webChannel = new QWebChannel;
    webcom = new WebCommunication();
    webChannel->registerObject("webcom", webcom);
    ui->graphicsMapsView->page()->setWebChannel(webChannel);

    connect(webcom, &WebCommunication::signalMouseClick, this, &MainWindow::setCurLocationInfo);
    connect(webcom, &WebCommunication::signalPositioning, this, &MainWindow::setAddressDescription);

}


MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::LoadMap(const QString &mapFile)
{
    QDir temDir(mapFile);
    QString absDir = temDir.absolutePath();
    QString filePath = "file:///" + absDir;
    ui->graphicsMapsView->load(QUrl(filePath));
}

void MainWindow::on_comboBoxMapsType_currentTextChanged(const QString &maptype)
{
    if(maptype.compare("谷歌地图") == 0) {
        ui->graphicsMapsView->load(QUrl(QStringLiteral("https://maps.google.com")));
    } else {
        LoadMap("map.html");
    }
}

void MainWindow::on_PositionBtn_clicked()
{
    QString cmd = QString("theLocation(%0, %1, function(address) {/*alert(address);*/ return address; })")
                      .arg(ui->lineEditLongitude->text())
                      .arg(ui->lineEditLatitude->text());
    qDebug() << "cmd:" << cmd;

    // Call JS function and handle the result
    ui->graphicsMapsView->page()->runJavaScript(cmd, [](const QVariant &v) {
        qDebug() << "Js result: " << v.toString();
    });

    // between C++ and Javascript are asynchronous with QtWebEngineView

    // QThread::msleep(200);
    // cmd = QString("getLocation()");
    // ui->graphicsMapsView->page()->runJavaScript(cmd, [this](const QVariant &v) {
    //     qDebug() << "Js result: " << v.toString();
    //     ui->lineEditCurAddress->setText(v.toString());
    // });
}

void MainWindow::setAddressDescription()
{
    qDebug() << "@setAddressDescription Js result: " << webcom->getJsBackData();
    ui->lineEditCurAddress->setText(webcom->getJsBackData());
}

void MainWindow::setCurLocationInfo()
{
    qDebug() << "@setCurLocationInfo Js result: " << webcom->getJsClickData();
    QStringList list = webcom->getJsClickData().split(";");
    ui->lineEditClickCurCoord->setText(list.at(0));
    ui->lineEditClickAddress->setText(list.at(1));
}


void MainWindow::on_queryRouteBtn_clicked()
{
    if(ui->lineEditStart->text().isEmpty() || ui->lineEditEnd->text().isEmpty()) {
        QMessageBox::warning(this,"warning","请设置起点或终点位置!",QMessageBox::Ok | QMessageBox::Cancel,QMessageBox::Cancel);
    }
    QString cmd = QString("routePlanning('%1', '%2', '%3')")
                      .arg(ui->lineEditStart->text())
                      .arg(ui->lineEditEnd->text())
                      .arg(m_travelMode);
    qDebug() << "cmd:" << cmd;
    ui->graphicsMapsView->page()->runJavaScript(cmd);
}


void MainWindow::on_addOverlayBtn_clicked()
{
    ui->graphicsMapsView->page()->runJavaScript("addOverlay()");
}


void MainWindow::on_clearOverlayBtn_clicked()
{
    ui->graphicsMapsView->page()->runJavaScript("removeOverlay()");
    // QString addres = "北京市海淀区上地10街10号";
    // QString cmd = QString("getPoint('%1')").arg(addres);
    // ui->graphicsMapsView->page()->runJavaScript(cmd);
}


void MainWindow::on_mapsTypeCheckBox_stateChanged()
{
    qDebug() << "cmd:" << "setMapStyle";
    ui->graphicsMapsView->page()->runJavaScript("setMapStyle()");
}

void MainWindow::on_zoomScaleCheckBox_stateChanged()
{
    qDebug() << "cmd:" << "removeControl";
    ui->graphicsMapsView->page()->runJavaScript("removeControl()");
}


void MainWindow::on_comboBoxTravelMode_currentTextChanged(const QString &travelMode)
{
    const QMap<QString, QString> travelModeMap = {
        {"驾车", "driving"},
        {"骑行", "bicycling"},
        {"步行", "walking"},
        {"公交", "transit"}
    };
    m_travelMode = travelModeMap[travelMode];
    qDebug() << m_travelMode;
}

